package com.rxkj.service.impl;

import com.rxkj.entity.rainwater.*;
import com.rxkj.mapper.WaterAndRainMapper;
import com.rxkj.service.WaterAndRainService;
import com.rxkj.utils.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WaterAndRainServiceImpl implements WaterAndRainService {

    @Autowired
    private WaterAndRainMapper waterAndRainMapper;


    @Override
    public PageData<WaterAndRainVO> getWaterAndRainOverview(int currentPage, int pageSize, String stnm) {
        Long totalRows = waterAndRainMapper.getDevTotalRows(stnm);
        PageData<WaterAndRainVO> res = new PageData<>(currentPage,pageSize,totalRows);
        List<WaterAndRainVO> qe = waterAndRainMapper.getDevWaterRainPage(res.getStart(), res.getEnd(), stnm);
        qe.forEach(warv -> {
            // 图片数据
            ImageSt ist = waterAndRainMapper.getImageByTermsn(warv.getTermsn());
            StationInfo sti = waterAndRainMapper.getStationxByTermsn(warv.getTermsn());
            warv.setImgpath(ist.getImgpath());
            warv.setImgname(ist.getImgname());
            warv.setXunxian(sti.getXunxian());
            warv.setKurong(sti.getKurong());
        });
        res.setRows(qe);
        return res;
    }

    @Override
    public PageData<RainSt> getRainsByTermsn(int currentPage, int pageSize, String termsn, String startTime, String endTime) {
        Long totalRows = waterAndRainMapper.getRainTotalRows(termsn, startTime, endTime);
        PageData<RainSt> res = new PageData<>(currentPage,pageSize,totalRows);
        List<RainSt> qe = waterAndRainMapper.getRainPage(res.getStart(), res.getEnd(), termsn, startTime, endTime);
        res.setRows(qe);
        return res;
    }

    @Override
    public PageData<WaterSt> getWatersByTermsn(int currentPage, int pageSize, String termsn, String startTime, String endTime) {
        Long totalRows = waterAndRainMapper.getWaterTotalRows(termsn, startTime, endTime);
        PageData<WaterSt> res = new PageData<>(currentPage,pageSize,totalRows);
        List<WaterSt> qe = waterAndRainMapper.getWaterPage(res.getStart(), res.getEnd(), termsn, startTime, endTime);
        res.setRows(qe);
        return res;
    }

    @Override
    public List<ImageSt> getImagesByTermsn(String termsn, String startTime, String endTime) {
        List<ImageSt> qe = waterAndRainMapper.getImagePage(termsn, startTime, endTime);
        return qe;
    }

    @Override
    public List<RainSt> getRainData(int timetype, String startTime, String endTime, String termsn) {
        List<RainSt> qe = new ArrayList<>();

        switch (timetype) {
            case 1:
                //5分钟
                qe = waterAndRainMapper.getRainData5(termsn, startTime, endTime);
                return qe;
            case 2:
                //1小时
                qe = waterAndRainMapper.getRainData60(termsn, startTime, endTime);
                return qe;
            case 3:
                //1天
                qe = waterAndRainMapper.getRainData24(termsn, startTime, endTime);
                if (qe.size() <= 0){
                    //查当天数据
                    qe = waterAndRainMapper.getRainDatax24(termsn, endTime);
                    return qe;
                }
                return qe;
            default:
                return qe;
        }
    }

    @Override
    public List<WaterSt> getWaterData(String termsn, String startTime, String endTime) {
        return waterAndRainMapper.getWaterData(termsn, startTime, endTime);
    }
}
